//
//  MoreInfoViewController.m
//  SmartRoadShow
//
//  Created by Tracy E on 10-10-8.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "MoreInfoViewController.h"
#import "ArtClass.h"
#import "DetailWebViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "MyDataSource.h"
#import "CacheManager.h"
#import "SendHttpRequest.h"

@implementation MoreInfoViewController


#pragma mark -
#pragma mark Initialization


- (void)getMoreInfoData:(NSArray *) infoArr{
	moreInfoArray = [[NSMutableArray alloc] initWithArray:infoArr];
	
	if ([moreInfoArray count] == 0) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请检查您的网络连接"
													   delegate:self
											  cancelButtonTitle:@"确定"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		
	}
}
#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	currentInfoPage = 0;
	
	self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.view.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.0];
	
    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 5, 310, 335) style:UITableViewStylePlain];
	[m_tableView setDelegate:self];
	[m_tableView setDataSource:self];
	[m_tableView setSeparatorColor:[UIColor clearColor]];
	m_tableView.layer.cornerRadius = 8;
	m_tableView.layer.masksToBounds =	YES;
	[m_tableView setBackgroundColor:[UIColor clearColor]];
	m_tableView.layer.contents = (id)[UIImage imageNamed:@"tableViewBackground.png"].CGImage;
	[self.view addSubview:m_tableView];
	
	
	

//	m_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(5, 10, 310, 40)];
//	
//	UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
//	[moreButton addTarget:self action:@selector(moreInfo) forControlEvents:UIControlEventTouchUpInside];
//	[moreButton setTitle:@"浏览更多..." forState:UIControlStateNormal];
//	[moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//	[moreButton setFrame:CGRectMake(105, 5, 100, 30)];
//	[m_tableView.tableFooterView addSubview:moreButton];
	
}

- (void)moreInfo{
	waitView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(80, 10, 20, 20)];
	[waitView setHidesWhenStopped:YES];
	[waitView startAnimating];
	[waitView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
	[m_tableView.tableFooterView addSubview:waitView];
	[waitView release];
	
	[self performSelector:@selector(getMoreInfoExecute) withObject:nil afterDelay:0.1];
	
}

- (void)getMoreInfoExecute{
	currentInfoPage++;

	[waitView stopAnimating];
	NSString *urlstring = @"http://img.mymb.com.cn/smartapp/artmap/ArtTourNews.php";
	NSString *postStr = [NSString stringWithFormat:@"page=%d",currentInfoPage];
	NSString *documentStr = [SendHttpRequest sendHttpPostRequestWithURLString:urlstring postString:postStr decodeEncoding:NSUTF8StringEncoding];
	NSArray *tempArr = [[MyDataSource getArtTourNews:documentStr] retain];

	if ([tempArr count] != 0) {
		[moreInfoArray addObjectsFromArray:tempArr];
	}
	[tempArr release];
	[m_tableView reloadData];
	
}

#pragma mark -
#pragma mark Table view data source


//- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView{
//	return [moreInfoArray count];
//}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	return [moreInfoArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
    MoreTableViewCell *cell = nil;
	if (cell == nil) {
		cell = [[[MoreTableViewCell alloc] init] autorelease];
		cell.accessoryType = UITableViewCellAccessoryNone;
		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
	cell.delegate = self;
	[cell setTag:indexPath.row];
	ArtClass *class = [moreInfoArray objectAtIndex:indexPath.row];
	[cell setImageWithURL:class._thumb];
	
	NSLog(@"image thunmb : %@",class._thumb);
	
	[cell setTitleLabelText:class._title];
	[cell setDetailLabelText:class._description];
	
	
	return cell;
}	

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{	
	return 130;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
	DetailWebViewController *moreInfoViewController = [[DetailWebViewController alloc] init];
//	[moreInfoViewController.webView loadHTMLString:[[moreInfoArray objectAtIndex:indexPath.row] _content] baseURL:nil];
	
	
	[moreInfoViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[moreInfoArray objectAtIndex:indexPath.row] _content]]]];
	
	[self.navigationController pushViewController:moreInfoViewController animated:YES];
	[moreInfoViewController release];
}

-(void)seeDetailInfo:(NSInteger) index{
	
	NSLog(@"index");
	
	DetailWebViewController *moreInfoViewController = [[DetailWebViewController alloc] init];
//	[moreInfoViewController.webView loadHTMLString:[[moreInfoArray objectAtIndex:index] _content] baseURL:nil];
	
	[moreInfoViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[moreInfoArray objectAtIndex:index] _content]]]];
	
	
	[self.navigationController pushViewController:moreInfoViewController animated:YES];
	[moreInfoViewController release];
	
}



#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
	m_tableView = nil;
	waitView = nil;
}


- (void)dealloc {
	[m_tableView release];
	[moreInfoArray release];
	[waitView release];
    [super dealloc];
}


@end

